﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;

namespace DAL
{
    public class LikeDAL
    {
        //根据商品ID以及用户ID查看商品是否已经收藏
        public static bool selectNumDAL(int userID, int goodsID)
        {
            string sql = string.Format("select COUNT(*) from tb_Like where UserID={0} and GoodsID={1}", userID, goodsID);
            int res = Convert.ToInt32(DBHelper.ExecuteScalar(sql));
            return res > 0;
        }
        ////根据商品ID以及用户ID删除
        public static bool deleteNumDAL(int userID,int goodsID)
        {
            string sql=string.Format("DELETE FROM tb_Like WHERE UserID={0} and GoodsID={1}",userID, goodsID);
            bool res=DBHelper.ExecuteNonQuery(sql);
            return res;
        }
        //根据收藏ID进行删除
        public static bool deleteByIDDAL(int LikeID)
        {
            string sql = string.Format("DELETE FROM tb_Like WHERE LikeID={0}", LikeID);
            bool res = DBHelper.ExecuteNonQuery(sql);
            return res;
        }
        //增加
        public static bool addDAL(int UserID, int goodsID)
        {
            string sql=string.Format("insert into tb_Like values ({0},{1})",UserID, goodsID);
            bool res = DBHelper.ExecuteNonQuery(sql);
            return res;
        }
        //查询收藏商品的信息，联表查询like_Goods
        public static List<Like_Goods> getAllDAL(int UserID)
        {
            string sql=string.Format("select l.LikeID,l.GoodsID,g.GoodsName,g.GoodsDesc,g.GoodsPic,g.GoodsPrice from tb_Like l,tb_Goods g where l.GoodsID=g.GoodsID and l.UserID={0}",UserID);
            List<Like_Goods> list = new List<Like_Goods>();
            SqlDataReader dr = DBHelper.GetDataReader(sql);
            while (dr.Read())
            {
                Like_Goods like_Goods= new Like_Goods();
                like_Goods.LikeID = dr.GetInt32(0);
                like_Goods.GoodsID= dr.GetInt32(1);
                like_Goods.GoodsName= dr.GetString(2);
                like_Goods.GoodsDesc= dr.GetString(3);
                like_Goods.GoodsPic= dr.GetString(4);
                like_Goods.GoodsPrice=dr.GetDecimal(5);
                list.Add(like_Goods);
            }
            dr.Close();
            return list;
        }
    }
}
